package com.lg.cms.service.impl;

import cn.hutool.db.handler.HandleHelper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lg.cms.entity.Category;
import com.lg.cms.mapper.CategoryMapper;
import com.lg.cms.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lg.cms.vo.news.CategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 文章（栏目）分类表 服务实现类
 * </p>
 *
 * @author lg
 * @since 2020-09-21
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @PostConstruct
    public void init(){
//        Category videoCategory = categoryMapper.selectOne(
//                new QueryWrapper<Category>().eq("name", "视频").eq("alias", "video"));
//        Category imageCategory = categoryMapper.selectOne(
//                new QueryWrapper<Category>().eq("name", "图片").eq("alias", "img"));
//        if(imageCategory == null){
//            imageCategory = new Category();
//            imageCategory.setSort(98);
//            imageCategory.setName("图片");
//            imageCategory.setAlias("img");
//            imageCategory.setDescription("图片栏目");
//            categoryMapper.insert(imageCategory);
//        }
//        if(videoCategory == null){
//            videoCategory = new Category();
//            videoCategory.setSort(99);
//            videoCategory.setName("视频");
//            videoCategory.setAlias("video");
//            videoCategory.setDescription("视频栏目");
//            categoryMapper.insert(videoCategory);
//        }

    }

    @Override
    public Map<String, Integer> getCategoryIdMap() {
        Map<String, Integer> map = new HashMap<>();
        categoryMapper.selectList(null).forEach(v -> map.put(v.getName(), v.getId()));
        return map;
    }

    /**
     * 获取栏目分类列表, 前台展示
     * @return
     */
    @Override
    public List<CategoryVO> getList() {
        List<CategoryVO> fuVoList = new ArrayList<>();
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort");
        wrapper.isNull("parent_id");
        List<Category> fuList =  categoryMapper.selectList(wrapper);
        for(Category c: fuList){
//            if(c.getName().equals("图片") || c.getName().equals("视频")){
//                continue;
//            }
            CategoryVO fuCategory = this.toCategoryVO(c, null);
            Category caa = new Category();
            List<Category> ziList = categoryMapper.selectList(new QueryWrapper<Category>().eq("parent_id", fuCategory.getCategoryId()).orderByAsc("sort"));
            List<CategoryVO> ziVoList = new ArrayList<>();
            for(Category zi: ziList){
                ziVoList.add(this.toCategoryVO(zi, c));
            }
            fuCategory.setChildren(ziVoList);
            fuVoList.add(fuCategory);

        }
        return fuVoList;
    }

    /**
     * 转为前台展示对象
     * @param category
     * @return
     */
    @Override
    public CategoryVO toCategoryVO(Category category, Category parent) {
        CategoryVO vo = new CategoryVO();
        vo.setName(category.getName());
        vo.setCategory(category);
        vo.setCategoryId(category.getId());
        vo.setUrl("/list/" + category.getId());
        if(parent != null) {
            vo.setParentAlias(parent.getAlias());
        }else{
            vo.setParentAlias(String.valueOf(System.currentTimeMillis()));
        }
        if(vo.getName().equals("视频")){
            vo.setUrl("/video_list/");
        }
        if(vo.getName().equals("图片")){
            vo.setUrl("/image_list/");
        }
        return vo;
    }
}
